using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 闪现Buff - 瞬间移动到指定位���
/// </summary>
public class BlinkBuff : BuffBase
{
    private Vector3 m_BlinkPosition;
    private float m_BlinkDistance;
    private bool m_HasBlinked;

    public BlinkBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is BlinkData blinkData)
        {
            m_BlinkDistance = blinkData.BlinkDistance;
        }
        else
        {
            m_BlinkDistance = 5.0f; // 默认闪现距离
        }
        
        // 计算闪现位置
        CalculateBlinkPosition();
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 执行闪现
        ExecuteBlink();
        
        // 播放闪现特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log($"{Target.name} 闪现到新位置");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 闪现是瞬间效果，不需要持续处���
    }

    private void CalculateBlinkPosition()
    {
        Vector3 currentPos = Target.transform.position;
        Vector3 blinkDirection;
        
        if (Caster != null)
        {
            // 闪现到施法者附���
            blinkDirection = (Caster.transform.position - currentPos).normalized;
        }
        else
        {
            // 默认向前闪现
            blinkDirection = Vector3.right;
        }
        
        m_BlinkPosition = currentPos + blinkDirection * m_BlinkDistance;
    }

    private void ExecuteBlink()
    {
        if (m_HasBlinked) return;
        
        m_HasBlinked = true;
        
        // 播放闪现前特���
        if (BuffData.Effect != null)
        {
            var startEffect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            startEffect.transform.localPosition = Vector3.zero;
        }
        
        // 瞬间移动到新位置
        Target.transform.position = m_BlinkPosition;
        
        // 播放闪现后特���
        if (BuffData.Effect != null)
        {
            var endEffect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            endEffect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetTrigger("blink");
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 闪现效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Blink"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 闪现效果不能叠加
    }
}
